#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class TrGoupArrangeItemProvider : CY.GFive.Core.Providers.DALProvider.ITrGoupArrangeItemProvider
	{
		#region Sql
		private static readonly string SqlSelectTrGoupArrangeItem = "SELECT [Id], [ArrangeItemCode],[Unit],[ClassCode],[WeekType],[Num],[AssessCate],[TeacherCode],[Turn],[ArrangeCode] FROM [TrGoupArrangeItem] WHERE  [Id]=@Id";

		private static readonly string SqlInsertTrGoupArrangeItem = "INSERT INTO [TrGoupArrangeItem]([ArrangeItemCode],[Unit],[ClassCode],[WeekType],[Num],[AssessCate],[TeacherCode],[Turn],[ArrangeCode]) values(@ArrangeItemCode,@Unit,@ClassCode,@WeekType,@Num,@AssessCate,@TeacherCode,@Turn,@ArrangeCode);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateTrGoupArrangeItem = "UPDATE [TrGoupArrangeItem] SET [ArrangeItemCode] = @ArrangeItemCode,[Unit] = @Unit,[ClassCode] = @ClassCode,[WeekType] = @WeekType,[Num] = @Num,[AssessCate] = @AssessCate,[TeacherCode] = @TeacherCode,[Turn] = @Turn,[ArrangeCode] = @ArrangeCode WHERE  [Id]=@Id";

		private static readonly string SqlDeleteTrGoupArrangeItem = "DELETE [TrGoupArrangeItem] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllTrGoupArrangeItem = "SELECT [Id], [ArrangeItemCode],[Unit],[ClassCode],[WeekType],[Num],[AssessCate],[TeacherCode],[Turn],[ArrangeCode] FROM [TrGoupArrangeItem]";
		#endregion

		#region ITrGoupArrangeItemProvider Members
		public Core.Business.TrGoupArrangeItem Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectTrGoupArrangeItem);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.TrGoupArrangeItem trGoupArrangeItem = new Core.Business.TrGoupArrangeItem();

				if (!reader.IsDBNull(0)) trGoupArrangeItem.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) trGoupArrangeItem.ArrangeItemCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) trGoupArrangeItem.Unit = reader.GetString(2);
				if (!reader.IsDBNull(3)) trGoupArrangeItem.ClassCode = reader.GetString(3);
				if (!reader.IsDBNull(4)) trGoupArrangeItem.WeekType = reader.GetInt32(4);
				if (!reader.IsDBNull(5)) trGoupArrangeItem.Num = reader.GetInt32(5);
				if (!reader.IsDBNull(6)) trGoupArrangeItem.AssessCate = reader.GetString(6);
				if (!reader.IsDBNull(7)) trGoupArrangeItem.TeacherCode = reader.GetString(7);
				if (!reader.IsDBNull(8)) trGoupArrangeItem.Turn = reader.GetString(8);
				if (!reader.IsDBNull(9)) trGoupArrangeItem.ArrangeCode = reader.GetString(9);

				return trGoupArrangeItem;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.TrGoupArrangeItem trGoupArrangeItem)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ArrangeItemCode", SqlDbType.NVarChar, trGoupArrangeItem.ArrangeItemCode);
			sql.AddParameter("@Unit", SqlDbType.NVarChar, trGoupArrangeItem.Unit);
			sql.AddParameter("@ClassCode", SqlDbType.NVarChar, trGoupArrangeItem.ClassCode);
			sql.AddParameter("@WeekType", SqlDbType.Int, trGoupArrangeItem.WeekType);
			sql.AddParameter("@Num", SqlDbType.Int, trGoupArrangeItem.Num);
			sql.AddParameter("@AssessCate", SqlDbType.NVarChar, trGoupArrangeItem.AssessCate);
			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, trGoupArrangeItem.TeacherCode);
			sql.AddParameter("@Turn", SqlDbType.NVarChar, trGoupArrangeItem.Turn);
			sql.AddParameter("@ArrangeCode", SqlDbType.NVarChar, trGoupArrangeItem.ArrangeCode);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertTrGoupArrangeItem);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) trGoupArrangeItem.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.TrGoupArrangeItem trGoupArrangeItem)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ArrangeItemCode", SqlDbType.NVarChar, trGoupArrangeItem.ArrangeItemCode);
			sql.AddParameter("@Unit", SqlDbType.NVarChar, trGoupArrangeItem.Unit);
			sql.AddParameter("@ClassCode", SqlDbType.NVarChar, trGoupArrangeItem.ClassCode);
			sql.AddParameter("@WeekType", SqlDbType.Int, trGoupArrangeItem.WeekType);
			sql.AddParameter("@Num", SqlDbType.Int, trGoupArrangeItem.Num);
			sql.AddParameter("@AssessCate", SqlDbType.NVarChar, trGoupArrangeItem.AssessCate);
			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, trGoupArrangeItem.TeacherCode);
			sql.AddParameter("@Turn", SqlDbType.NVarChar, trGoupArrangeItem.Turn);
			sql.AddParameter("@ArrangeCode", SqlDbType.NVarChar, trGoupArrangeItem.ArrangeCode);
			sql.AddParameter("@Id", SqlDbType.Int, trGoupArrangeItem.Id);
			sql.ExecuteSql(SqlUpdateTrGoupArrangeItem);
		}

		public void Delete(Core.Business.TrGoupArrangeItem trGoupArrangeItem)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, trGoupArrangeItem.Id);
			sql.ExecuteSql(SqlDeleteTrGoupArrangeItem);
		}

		public IList<Core.Business.TrGoupArrangeItem> GetAllTrGoupArrangeItem()
		{
			IList<Core.Business.TrGoupArrangeItem> trGoupArrangeItemlist = new List<Core.Business.TrGoupArrangeItem>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllTrGoupArrangeItem);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.TrGoupArrangeItem trGoupArrangeItem = new Core.Business.TrGoupArrangeItem();

					if (!reader.IsDBNull(0)) trGoupArrangeItem.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) trGoupArrangeItem.ArrangeItemCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) trGoupArrangeItem.Unit = reader.GetString(2);
					if (!reader.IsDBNull(3)) trGoupArrangeItem.ClassCode = reader.GetString(3);
					if (!reader.IsDBNull(4)) trGoupArrangeItem.WeekType = reader.GetInt32(4);
					if (!reader.IsDBNull(5)) trGoupArrangeItem.Num = reader.GetInt32(5);
					if (!reader.IsDBNull(6)) trGoupArrangeItem.AssessCate = reader.GetString(6);
					if (!reader.IsDBNull(7)) trGoupArrangeItem.TeacherCode = reader.GetString(7);
					if (!reader.IsDBNull(8)) trGoupArrangeItem.Turn = reader.GetString(8);
					if (!reader.IsDBNull(9)) trGoupArrangeItem.ArrangeCode = reader.GetString(9);

					trGoupArrangeItem.MarkOld();
					trGoupArrangeItemlist.Add(trGoupArrangeItem);
				}
				reader.Close();
			}
			return trGoupArrangeItemlist;
		}
		#endregion
	}
}
